#region FILE DESCRIPTION
//Author Tam Nguyen
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using GameFramework.Graphics;

namespace GameFramework.Resources
{
    public class Resource<T>
    {
        Dictionary<String, T> _data;
        Dictionary<String, String> _memory;
        ContentManager _content;

        int _loadedIndex;
        public int LoadedIndex
        {
            get
            {
                return _loadedIndex;
            }
            set
            {
                _loadedIndex = value;
            }
        }

        public Resource(ContentManager content)
        {
            _data = new Dictionary<string, T>();
            _memory = new Dictionary<string, string>();
            _loadedIndex = 0;
            _content = content;
        }

        public void Add(String name, String path)
        {
            if (_data.ContainsKey(name))
            {
                System.Diagnostics.Debug.Assert(true, "Resource not contrains in memory.");
                return;
            }
            else
            {
                _memory.Add(name, path);
            }
        }

        public void Add(String name, T data)
        {
            _data.Add(name, data);
        }


        public T Get(String name)
        {
            if (_data.ContainsKey(name))
            {
                return _data[name];
            }
            else
            {
                System.Diagnostics.Debug.Assert(true, "Resource not contrains in memory.");
                return default(T);
            }
        }

        public bool Load()
        {
            if (_loadedIndex == TotalItems)
            {
                return true;
            }
            else
            {
                KeyValuePair<String, String> memoryItem = _memory.ElementAt(_loadedIndex);
                T newData = _content.Load<T>(memoryItem.Value);
                _data.Add(memoryItem.Key, newData);
                _loadedIndex++;
                return false;                              
            }
        }

        public bool GetMemoryItem(ref KeyValuePair<String,String> value)
        {
            if (_loadedIndex < _memory.Count)
            {
                value = _memory.ElementAt(_loadedIndex);
                return true;
            }
            return false;
        }

        public Dictionary<String, T> GetDictionaryData()
        {
            return _data;
        }        

        public int TotalItems
        {
            get
            {
                return _memory.Count;
            }
        }
    }
}
